/************************************  

DESCRIPTION: This program estimates the blocking estimator for women. 
 Note that the women sample is larger than the men sample and estimated more of the blocks in chunks.
 Some chunks represented in the program. Others are not.
 
 We use Program 3b.Blocking_Female_combined.do to put together the chunks.

Datasets Used:
 (1) femAllBlocks
 (2) femDiabBlocks
 (3) femHypBlocks
 (4) femLPManalysis2

Datasets Created:
 (1) fembetaBlockDiab
 (2) fembetaBlockHyp_lt20
 (3) fembetaBlockHyp_gt20
	(fembetaBlockHyp_2034 fembetaBlockHyp_gt34) 
 (4) fembetaBlockDeath 
    
 
************************************/  

set more off
capture log close
clear all

set matsize 10000
*global base "N:\"
global base "\\192.168.6.107\silstorage\"
cd "$base\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Program\PropensityScore\Patients\ResponsetoReferee\OtherPropScoreMethods\Blocking"
global origData "$base\MedicareClaims-P045601-BE"
*Use data from LPM
global dataIn "$base\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Program\PropensityScore\Patients\ResponsetoReferee\OtherPropScoreMethods\LPM"
global dataOut "$base\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out"
global dpath "$base\MedicareClaims-P045601-BE\Work\ay_data"
global dataProp "$base\\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out\PropScore\Patients"
global logs "$base\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Logs\PropScore\Patients"
global dpath "$base\\MedicareClaims-P045601-BE\Work\ay_data"
global skapath "$base\\MedicareClaims-P045601-BE\Work\ska"
 
log using "$logs/2b.BlockingEstimator_Regression.log", replace


	*Columnames program

	program getColnames 
	 quietly {
	  matrix A = e(b)'
	 
	 local rownames : rowfullnames A
	 local c : word count `rownames'
	 local names : colfullnames A

	 svmat A, names(col)

	 gen rownames = ""
	 forvalues i = 1/`c' {

	  replace rownames = "`:word `i' of `rownames''" in `i' 
	   } 

	 }

	end


	program getRegStats
	 quietly {
	  matrix A = e(b)'
	 
	 local rownames : rowfullnames A
	 local c : word count `rownames'
	 local n = `c' + 1
	 local rsq = `c' + 3

	 replace beta1 =e(N) in `n' 
	 replace beta1 =e(r2_p) in `rsq' 

	 }

	end
	
*Setup blocking data. VMERGER is on both datasets. don't want to overwrite either one
	*Death	
	 use ./femAllBlocks.dta
		 sort BENE_ID 
			 capture rename EHAT ehat
			 capture rename vmerger VMERGER
		 save ./femAllBlocks.dta, replace
	 clear
	 
	 *Diabetes
	 use ./femDiabBlocks.dta
		 sort BENE_ID 
			 capture rename EHAT ehat
			 capture rename vmerger VMERGER
		 save ./femDiabBlocks.dta, replace
	 clear
	 
	 *Hypertension
	 use ./femHypBlocks.dta
		 sort BENE_ID 
			 capture rename EHAT ehat
			 capture rename vmerger VMERGER
		 save ./femHypBlocks.dta, replace
	 clear

 *Hypertension 

  use $dataIn/femLPManalysis2.dta
	 drop if mxhypertension_p_all==1 & mxicd8_chronic_p_all==0
	  drop if ehat ==.
	  keep if mxhypertension_p_all ==1
	  capture drop urban_rural
		  gen urban_rural=urbanrural1+2*urbanrural2
		   replace urban_rural=3 if urban_rural==0
		  capture rename mxicd8_chronic mxicd8_cardiac
	  capture drop spec35 specrt spec97 spec66 spec04 spec16 spec05 spec43 spec08 spec01 spec11 spec93 spec83 spec39 spec66 spec04 spec16 
	  capture drop _merge
		  gen zip3 = substr(zip_pat,1,3)   
		  encode zip3, gen(zip3n)	 
	  
	sort BENE_ID
	merge m:1 BENE_ID using ./femHypBlocks.dta
	  drop ehat VMERGER
	  drop if block >20 

	*Blocks less than 20	  
		 levelsof block if block <20, local(lblock)
		 
		 foreach b of local lblock {
		 
		*HYPERTENSION 
			 foreach var of varlist oc_acuteicd8 oc_ami oc_ihd oc_death {
				 if regexm("`var'","oc_([a-z0-9]+)$") { 
				  local pre = "`=regexs(1)'"
				 }
			 
			 di "`pre'" " " "`b'"
			 
			 capture rename mxicd3_endocrine mxicd3_chronic 
			 capture rename mxicd8_chronic mxicd8_cardiac
			 
			areg `var' i.postmerger  i.vmerger  i.vmerger#c.nyear i.age##i.rac_white i.qtr##i.urban_rural mxicd1_acute mxicd5_acute mxicd18_acute mxicd*_c* fmp nptf anesth spec* fs* lnvisit* if block==`b' & kv_`pre'==1, abs(zipwhite) cluster(zip3)   

			  matrix beta  = e(b)' 
			  matrix sesq = vecdiag(e(V))' 
			  svmat beta
			  getColnames
			  getRegStats
			  svmat sesq
			  rename rownames rn`pre'_`b'
			  capture drop y1 
			  capture drop j 
			  capture drop i
			  rename beta1 beta_`pre'_`b'
			  rename sesq1 sesq_`pre'_`b'
			 
			 
			  matrix B = e(b)
			  local nvars = colsof(B)
			 
			  replace rn`pre'_`b' = "N" if _n == `nvars' + 1
			  replace beta_`pre'_`b' = e(N) if _n == `nvars' + 1
			 
			  replace rn`pre'_`b' = "R2" if _n == `nvars' + 2
			  replace beta_`pre'_`b' = e(r2) if _n == `nvars' + 2
			 
			  replace rn`pre'_`b' = "aR2" if _n == `nvars' + 3
			  replace beta_`pre'_`b' = e(r2_a) if _n == `nvars' + 3
			 
			  
			  }
			}
	 keep rn* beta* sesq*
	 drop if _n>1000
	 save ./fembetaBlockHyp_lt20.dta, replace
	 clear
 

*Hypertension Blocks >=20
   use $dataIn/femLPManalysis2.dta
	 drop if mxhypertension_p_all==1 & mxicd8_chronic_p_all==0
	  drop if ehat ==.
	  keep if mxhypertension_p_all ==1
	  capture drop urban_rural
	  gen urban_rural=urbanrural1+2*urbanrural2
	  replace urban_rural=3 if urban_rural==0
	  capture rename mxicd8_chronic mxicd8_cardiac
	  capture drop spec35 specrt spec97 spec66 spec04 spec16 spec05 spec43 spec08 spec01 spec11 spec93 spec83 spec39 spec66 spec04 spec16 
	  capture drop _merge
	  gen zip3 = substr(zip_pat,1,3)   
	  encode zip3, gen(zip3n)	 
	  
	sort BENE_ID
	merge m:1 BENE_ID using ./femHypBlocks.dta
	  drop ehat VMERGER
	 keep if _merge==3
	 drop if block<20
 
		 levelsof block if block >=20, local(lblock)
		 
		 foreach b of local lblock {
		 
		*Hypertension 
			 foreach var of varlist oc_acuteicd8 oc_ami oc_ihd oc_death {
				 if regexm("`var'","oc_([a-z0-9]+)$") { 
				  local pre = "`=regexs(1)'"
				 }
			 
			 di "`pre'" " " "`b'"
			 
			 capture rename mxicd3_endocrine mxicd3_chronic 
			 capture rename mxicd8_chronic mxicd8_cardiac
			 
			areg `var' i.postmerger  i.vmerger  i.vmerger#c.nyear i.age##i.rac_white i.qtr##i.urban_rural mxicd1_acute mxicd5_acute mxicd18_acute mxicd*_c* fmp nptf anesth spec* fs* lnvisit* if block==`b' & kv_`pre'==1, abs(zipwhite) cluster(zip3)   

			  matrix beta  = e(b)' 
			  matrix sesq = vecdiag(e(V))' 
			  svmat beta
			  getColnames
			  getRegStats
			  svmat sesq
			  rename rownames rn`pre'_`b'
			  capture drop y1 
			  capture drop j 
			  capture drop i
			  rename beta1 beta_`pre'_`b'
			  rename sesq1 sesq_`pre'_`b'
			 
			 
			  matrix B = e(b)
			  local nvars = colsof(B)
			 
			  replace rn`pre'_`b' = "N" if _n == `nvars' + 1
			  replace beta_`pre'_`b' = e(N) if _n == `nvars' + 1
			 
			  replace rn`pre'_`b' = "R2" if _n == `nvars' + 2
			  replace beta_`pre'_`b' = e(r2) if _n == `nvars' + 2
			 
			  replace rn`pre'_`b' = "aR2" if _n == `nvars' + 3
			  replace beta_`pre'_`b' = e(r2_a) if _n == `nvars' + 3
			 
			  }
			}
	 keep rn* beta* sesq*
	 drop if _n>1000
	 save ./fembetaBlockHyp_gt20.dta, replace
	 clear
 
 *Death
	use $dataIn/femLPManalysis2.dta
	 drop if mxhypertension_p_all==1 & mxicd8_chronic_p_all==0
	  drop if ehat ==.
	  capture drop urban_rural
	  gen urban_rural=urbanrural1+2*urbanrural2
	  replace urban_rural=3 if urban_rural==0
	  capture rename mxicd8_chronic mxicd8_cardiac
	  capture drop spec35 specrt spec97 spec66 spec04 spec16 spec05 spec43 spec08 spec01 spec11 spec93 spec83 spec39 spec66 spec04 spec16 
	  capture drop _merge
	  gen zip3 = substr(zip_pat,1,3)   
	  encode zip3, gen(zip3n)	 

	 sort BENE_ID
	 capture drop _merge
	merge m:1 BENE_ID using ./femAllBlocks.dta
	  drop ehat VMERGER

	 levelsof block if block, local(lblock)
	 
	 foreach b of local lblock {
	 
	*Death
	 foreach var of varlist oc_death {
	 if regexm("`var'","oc_([a-z0-9]+)$") { 
	  local pre = "`=regexs(1)'"
	 }
	 
	 di "`pre'" " " "`b'"
	 
	 capture rename mxicd3_endocrine mxicd3_chronic 
	 capture rename mxicd8_chronic mxicd8_cardiac
	 
	areg `var' i.postmerger  i.vmerger  i.vmerger#c.nyear i.age##i.rac_white i.qtr##i.urban_rural mxicd1_acute mxicd5_acute mxicd18_acute mxicd*_c* fmp nptf anesth spec* fs* lnvisit* if block==`b' & kv_`pre'==1, abs(zipwhite) cluster(zip3)   

	  matrix beta  = e(b)' 
	  matrix sesq = vecdiag(e(V))' 
	  svmat beta
	  getColnames
	  getRegStats
	  svmat sesq
	  rename rownames rn`pre'_`b'
	  capture drop y1 
	  capture drop j 
	  capture drop i
	  rename beta1 beta_`pre'_`b'
	  rename sesq1 sesq_`pre'_`b'
	 
	 
	  matrix B = e(b)
	  local nvars = colsof(B)
	 
	  replace rn`pre'_`b' = "N" if _n == `nvars' + 1
	  replace beta_`pre'_`b' = e(N) if _n == `nvars' + 1
	 
	  replace rn`pre'_`b' = "R2" if _n == `nvars' + 2
	  replace beta_`pre'_`b' = e(r2) if _n == `nvars' + 2
	 
	  replace rn`pre'_`b' = "aR2" if _n == `nvars' + 3
	  replace beta_`pre'_`b' = e(r2_a) if _n == `nvars' + 3
	 
	  
	  }
	}
	 keep rn* beta* sesq*
	 drop if _n>1000
	 save ./fembetaBlockDeath.dta, replace
	 clear
	 
	 
	 *Diabetes
	  *Run Linear probability analysis
	  use $dataIn/femLPManalysis.dta
	 *Drop these contradictions
	 drop if mxhypertension_p_all==1 & mxicd8_chronic_p_all==0
	  drop if ehat ==.
	  keep if mxdiabetes_p_all ==1
	  capture drop urban_rural
	  gen urban_rural=urbanrural1+2*urbanrural2
	  replace urban_rural=3 if urban_rural==0
	  capture rename mxicd8_chronic mxicd8_cardiac
	  capture drop spec35 specrt spec97 spec66 spec04 spec16 spec05 spec43 spec08 spec01 spec11 spec93 spec83 spec39 spec66 spec04 spec16 
	  capture drop _merge
	  gen zip3 = substr(zip_pat,1,3)   
	  encode zip3, gen(zip3n)	 
		 
	sort BENE_ID
	merge m:1 BENE_ID using ./femDiabBlocks.dta
	  drop ehat VMERGER
	 
	*Diabetes 
		 levelsof block , local(lblock)
		 
		 foreach b of local lblock {
			 foreach var of varlist oc_diabcomp2 oc_diabcomp1 oc_glaucoma oc_death {
			 
				 if regexm("`var'","oc_([a-z0-9]+)$") { 
				  local pre = "`=regexs(1)'"
				 }
			 
			 di "`pre'" " " "`b'"
			 
			 capture rename mxicd3_chronic mxicd3_endocrine 
			 
			areg `var' i.postmerger  i.vmerger  i.vmerger#c.nyear i.age##i.rac_white i.qtr##i.urban_rural mxicd1_acute mxicd5_acute mxicd18_acute mxicd*_c* fmp nptf anesth spec* fs* lnvisit* if mxdiabetes_p_all==1 & block==`b' & kv_`pre'==1, abs(zipwhite) cluster(zip3)   

			  matrix beta  = e(b)' 
			  matrix sesq = vecdiag(e(V))' 
			  svmat beta
			  getColnames
			  getRegStats
			  svmat sesq
			  rename rownames rn`pre'_`b'
			  capture drop y1 
			  capture drop j 
			  capture drop i
			  rename beta1 beta_`pre'_`b'
			  rename sesq1 sesq_`pre'_`b'
			 
			 
			  matrix B = e(b)
			  local nvars = colsof(B)
			 
			  replace rn`pre'_`b' = "N" if _n == `nvars' + 1
			  replace beta_`pre'_`b' = e(N) if _n == `nvars' + 1
			 
			  replace rn`pre'_`b' = "R2" if _n == `nvars' + 2
			  replace beta_`pre'_`b' = e(r2) if _n == `nvars' + 2
			 
			  replace rn`pre'_`b' = "aR2" if _n == `nvars' + 3
			  replace beta_`pre'_`b' = e(r2_a) if _n == `nvars' + 3
			 
			  }
		 }
	 
	 keep rn* beta* sesq*
	 drop if _n>1000
	 save ./fembetaBlockDiab.dta, replace
	 clear
